﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Add_sensorForm : Form
    {
        ListViewItem editItem = null;
        Monitor_DB add_db = Monitor_DB.instance;
        sensor_type add_sensorType;
        ListView parent_listView1 = null;
        bool b_add = true;

        public Add_sensorForm(ListViewItem addItem, sensor_type s_type, MonitorForm parent, bool add)
        {
            InitializeComponent();

            editItem = addItem;
            add_sensorType = s_type;
            b_add = add;

            if (parent != null)
            {
                parent_listView1 = parent.Get_MonitorForm_ListView1();
            }

        }

        private void button_enter_Click(object sender, EventArgs e)
        {
            try
            {
                if (b_add)
                {
                    //添加项
                    switch (add_sensorType)
                    {
                        case sensor_type.tempture:

                            tb_tempture tem = new tb_tempture();
                            tem.MonitorDevice_Id = Convert.ToInt16(editItem.SubItems[0].Name);
                            tem.Name = textBox_name.Text;

                            if (textBox_threshold.Text != "")
                            {
                                tem.Threshold = Convert.ToInt16(textBox_threshold.Text);
                            }

                            if (textBox_threshold2.Text != "")
                            {
                                tem.Threshold_Two = Convert.ToInt16(textBox_threshold2.Text);
                            }

                            if (textBox_threshold3.Text != "")
                            {
                                tem.Threshold_Three = Convert.ToInt16(textBox_threshold3.Text);
                            }

                            tem.Status = textBox_status.Text;
                            if (this.checkBox_inUse.Checked == true)
                            {
                                tem.InUse = true;
                            }
                            else
                            {
                                tem.InUse = false;
                            }

                            add_db.db.tb_tempture.InsertOnSubmit(tem);
                            add_db.db.SubmitChanges(ConflictMode.FailOnFirstConflict);

                            if (parent_listView1 != null)
                            {
                                ListViewItem i = new ListViewItem(textBox_monitorDeviceID.Text);
                                i.SubItems[0].Name = editItem.SubItems[0].Name;
                                i.SubItems[0].Text = editItem.SubItems[0].Text;

                                i.ImageIndex = 6;
                                i.SubItems.Add(tem.ID.ToString());
                                i.SubItems.Add(textBox_name.Text);
                                i.SubItems.Add(textBox_zone.Text);
                                i.SubItems.Add(textBox_status.Text);

                                i.SubItems.Add(textBox_threshold.Text);
                                i.SubItems.Add(textBox_threshold2.Text);
                                i.SubItems.Add(textBox_threshold3.Text);

                                // Value
                                i.SubItems.Add("");
                                i.SubItems.Add(this.checkBox_inUse.Checked.ToString());

                                parent_listView1.Items.Add(i);
                            }
                            break;

                        case sensor_type.humidity:
                            tb_humidity hum = new tb_humidity();
                            hum.MonitorDevice_Id = Convert.ToInt16(editItem.SubItems[0].Name);
                            hum.Name = textBox_name.Text;

                            if (textBox_threshold.Text != "")
                            {
                                hum.Threshold = Convert.ToInt16(textBox_threshold.Text);
                            }

                            if (textBox_threshold2.Text != "")
                            {
                                hum.Threshold_Two = Convert.ToInt16(textBox_threshold2.Text);
                            }

                            if (textBox_threshold3.Text != "")
                            {
                                hum.Threshold_Three = Convert.ToInt16(textBox_threshold3.Text);
                            }

                            hum.Status = textBox_status.Text;
                            if (this.checkBox_inUse.Checked == true)
                            {
                                hum.InUse = true;
                            }
                            else
                            {
                                hum.InUse = false;
                            }

                            add_db.db.tb_humidity.InsertOnSubmit(hum);
                            add_db.db.SubmitChanges(ConflictMode.FailOnFirstConflict);

                            if (parent_listView1 != null)
                            {
                                ListViewItem i = new ListViewItem(textBox_monitorDeviceID.Text);
                                i.SubItems[0].Name = editItem.SubItems[0].Name;
                                i.SubItems[0].Text = editItem.SubItems[0].Text;

                                i.ImageIndex = 4;
                                i.SubItems.Add(hum.ID.ToString());
                                i.SubItems.Add(textBox_name.Text);
                                i.SubItems.Add(textBox_zone.Text);
                                i.SubItems.Add(textBox_status.Text);

                                i.SubItems.Add(textBox_threshold.Text);
                                i.SubItems.Add(textBox_threshold2.Text);
                                i.SubItems.Add(textBox_threshold3.Text);

                                // Value
                                i.SubItems.Add("");
                                i.SubItems.Add(this.checkBox_inUse.Checked.ToString());

                                parent_listView1.Items.Add(i);
                            }

                            break;

                        case sensor_type.shake:
                            tb_shake sha = new tb_shake();
                            sha.MonitorDevice_Id = Convert.ToInt16(editItem.SubItems[0].Name);
                            sha.Name = textBox_name.Text;

                            if (textBox_threshold.Text != "")
                            {
                                sha.Threshold = Convert.ToInt16(textBox_threshold.Text);
                            }

                            if (textBox_threshold2.Text != "")
                            {
                                sha.Threshold_Two = Convert.ToInt16(textBox_threshold2.Text);
                            }

                            if (textBox_threshold3.Text != "")
                            {
                                sha.Threshold_Three = Convert.ToInt16(textBox_threshold3.Text);
                            }

                            sha.Status = textBox_status.Text;
                            if (this.checkBox_inUse.Checked == true)
                            {
                                sha.InUse = true;
                            }
                            else
                            {
                                sha.InUse = false;
                            }

                            add_db.db.tb_shake.InsertOnSubmit(sha);
                            add_db.db.SubmitChanges(ConflictMode.FailOnFirstConflict);

                            if (parent_listView1 != null)
                            {
                                ListViewItem i = new ListViewItem(textBox_monitorDeviceID.Text);
                                i.SubItems[0].Name = editItem.SubItems[0].Name;
                                i.SubItems[0].Text = editItem.SubItems[0].Text;

                                i.ImageIndex = 5;
                                i.SubItems.Add(sha.ID.ToString());
                                i.SubItems.Add(textBox_name.Text);
                                i.SubItems.Add(textBox_zone.Text);
                                i.SubItems.Add(textBox_status.Text);

                                i.SubItems.Add(textBox_threshold.Text);
                                i.SubItems.Add(textBox_threshold2.Text);
                                i.SubItems.Add(textBox_threshold3.Text);

                                // Value
                                i.SubItems.Add("");
                                i.SubItems.Add(this.checkBox_inUse.Checked.ToString());

                                parent_listView1.Items.Add(i);
                            }

                            break;

                        case sensor_type.voltage:
                            tb_voltage vol = new tb_voltage();
                            vol.MonitorDevice_Id = Convert.ToInt16(editItem.SubItems[0].Name);
                            
                            vol.Name = textBox_name.Text;

                            if (textBox_threshold.Text != "")
                            {
                                vol.Threshold = Convert.ToInt16(textBox_threshold.Text);
                            }

                            if (textBox_threshold2.Text != "")
                            {
                                vol.Threshold_Two = Convert.ToInt16(textBox_threshold2.Text);
                            }

                            if (textBox_threshold3.Text != "")
                            {
                                vol.Threshold_Three = Convert.ToInt16(textBox_threshold3.Text);
                            }

                            vol.Status = textBox_status.Text;
                            if (this.checkBox_inUse.Checked == true)
                            {
                                vol.InUse = true;
                            }
                            else
                            {
                                vol.InUse = false;
                            }

                            add_db.db.tb_voltage.InsertOnSubmit(vol);
                            add_db.db.SubmitChanges(ConflictMode.FailOnFirstConflict);

                            if (parent_listView1 != null)
                            {
                                ListViewItem i = new ListViewItem(textBox_monitorDeviceID.Text);
                                i.SubItems[0].Name = editItem.SubItems[0].Name;
                                i.SubItems[0].Text = editItem.SubItems[0].Text;

                                i.ImageIndex = 7;
                                i.SubItems.Add(vol.ID.ToString());
                                i.SubItems.Add(textBox_name.Text);
                                i.SubItems.Add(textBox_zone.Text);
                                i.SubItems.Add(textBox_status.Text);

                                i.SubItems.Add(textBox_threshold.Text);
                                i.SubItems.Add(textBox_threshold2.Text);
                                i.SubItems.Add(textBox_threshold3.Text);

                                // Value
                                i.SubItems.Add("");
                                i.SubItems.Add(this.checkBox_inUse.Checked.ToString());

                                parent_listView1.Items.Add(i);
                            }

                            break;

                        case sensor_type.door:
                            tb_door door = new tb_door();
                            door.MonitorDevice_Id = Convert.ToInt16(editItem.SubItems[0].Name);

                            door.Name = textBox_name.Text;
                            if (this.checkBox_doorOpen.Checked == true)
                            {
                                door.Status = true;
                            }
                            else if (this.checkBox_doorClose.Checked == true)
                            {
                                door.Status = false;
                            }

                            if (this.checkBox_frontDoor.Checked == true)
                            {
                                door.Door_Type = "前";
                            }
                            else if (this.checkBox_backDoor.Checked == true)
                            {
                                door.Door_Type = "后";
                            }

                            if (this.checkBox_inUse.Checked == true)
                            {
                                door.InUse = true;
                            }
                            else
                            {
                                door.InUse = false;
                            }

                            add_db.db.tb_door.InsertOnSubmit(door);
                            add_db.db.SubmitChanges(ConflictMode.FailOnFirstConflict);

                            if (parent_listView1 != null)
                            {
                                ListViewItem i = new ListViewItem(textBox_monitorDeviceID.Text);
                                i.SubItems[0].Name = editItem.SubItems[0].Name;
                                i.SubItems[0].Text = editItem.SubItems[0].Text;

                                i.ImageIndex = 3;

                                i.SubItems.Add(door.ID.ToString());
                                i.SubItems.Add(textBox_name.Text);
                                i.SubItems.Add(textBox_zone.Text);

                                if (this.checkBox_frontDoor.Checked == true)
                                {
                                    i.SubItems.Add("前");
                                }
                                else
                                {
                                    i.SubItems.Add("后");
                                }

                                if (this.checkBox_doorOpen.Checked == true)
                                {
                                    i.SubItems.Add("开");
                                }
                                else
                                {
                                    i.SubItems.Add("关");
                                }

                                i.SubItems.Add(this.checkBox_inUse.Checked.ToString());

                                parent_listView1.Items.Add(i);
                            }

                            break;

                        default:
                            break;
                    }
                }
                else
                {
                    //编辑项
                    MONITOR_DBDataContext db = Monitor_DB.instance.db;
                    if(db == null)
                    {
                        MessageBox.Show("Data Base is NULL!");
                    }

                    int deviceId = Convert.ToInt32(textBox_deviceID.Text);
                   
                    switch (add_sensorType)
                    {
                        case sensor_type.door:
                            IEnumerable<tb_door> door =
                                from doo in db.tb_door
                                where doo.ID == deviceId
                                select doo;

                            tb_door door_Item = null;
                            foreach (tb_door item in door)
                            {
                                door_Item = item;
                                break;
                            }

                            if (door_Item != null)
                            {
                                door_Item.Name = textBox_name.Text;
                                if (this.checkBox_doorOpen.Checked == true)
                                {
                                    door_Item.Status = true;
                                }
                                else if (this.checkBox_doorClose.Checked == true)
                                {
                                    door_Item.Status = false;
                                }

                                if (this.checkBox_frontDoor.Checked == true)
                                {
                                    door_Item.Door_Type = "前";
                                }
                                else if (this.checkBox_backDoor.Checked == true)
                                {
                                    door_Item.Door_Type = "后";
                                }

                                if (this.checkBox_inUse.Checked == true)
                                {
                                    door_Item.InUse = true;
                                }
                                else
                                {
                                    door_Item.InUse = false;
                                }

                                add_db.db.SubmitChanges(ConflictMode.FailOnFirstConflict);

                                editItem.SubItems[2].Text = textBox_name.Text;

                                if (this.checkBox_frontDoor.Checked == true)
                                {
                                    editItem.SubItems[4].Text = "前";
                                }
                                else if (this.checkBox_backDoor.Checked == true)
                                {
                                    editItem.SubItems[4].Text = "后";
                                }

                                if (this.checkBox_doorOpen.Checked == true)
                                {
                                    editItem.SubItems[5].Text = "开";
                                }
                                else if (this.checkBox_doorClose.Checked == true)
                                {
                                    editItem.SubItems[5].Text = "关";
                                }

                                if (this.checkBox_inUse.Checked == true)
                                {
                                    editItem.SubItems[6].Text = "True";
                                }
                                else
                                {
                                    editItem.SubItems[6].Text = "False";
                                }
                            }
                            else
                            {
                                MessageBox.Show("Can't find element in DB!");
                            }

                            break;

                        case sensor_type.tempture:

                            IEnumerable<tb_tempture> temp =
                               from tem in db.tb_tempture
                               where tem.ID == deviceId
                               select tem;

                            tb_tempture tem_Item = null;
                            foreach (tb_tempture item in temp)
                            {
                                tem_Item = item;
                                break;
                            }

                            if (tem_Item != null)
                            {
                                tem_Item.Name = textBox_name.Text;
                                tem_Item.Status = textBox_status.Text;

                                if (textBox_threshold.Text != "")
                                {
                                    tem_Item.Threshold = Convert.ToInt16(textBox_threshold.Text);
                                }

                                if (textBox_threshold2.Text != "")
                                {
                                    tem_Item.Threshold_Two = Convert.ToInt16(textBox_threshold2.Text);
                                }

                                if (textBox_threshold3.Text != "")
                                {
                                    tem_Item.Threshold_Three = Convert.ToInt16(textBox_threshold3.Text);
                                }

                                if (this.checkBox_inUse.Checked == true)
                                {
                                    tem_Item.InUse = true;
                                }
                                else
                                {
                                    tem_Item.InUse = false;
                                }

                                add_db.db.SubmitChanges(ConflictMode.FailOnFirstConflict);

                                editItem.SubItems[2].Text = textBox_name.Text;
                                editItem.SubItems[4].Text = textBox_status.Text;
                                editItem.SubItems[5].Text = textBox_threshold.Text;
                                editItem.SubItems[6].Text = textBox_threshold2.Text;
                                editItem.SubItems[7].Text = textBox_threshold3.Text;

                                if (this.checkBox_inUse.Checked == true)
                                {
                                    editItem.SubItems[9].Text = "True";
                                }
                                else
                                {
                                    editItem.SubItems[9].Text = "False";
                                }

                                // Alarm Type
                                alarm_type alarmType = Alarm.alarmDetect.instance.DetermineAlarmType(add_sensorType, Convert.ToDouble(tem_Item.Value), Convert.ToDouble(tem_Item.Threshold),
                                    Convert.ToDouble(tem_Item.Threshold_Two), Convert.ToDouble(tem_Item.Threshold_Three));


                                // Set Row background color
                                switch (alarmType)
                                {
                                    case alarm_type.normal:
                                        editItem.BackColor = Color.LightCyan;
                                        break;

                                    case alarm_type.mini:
                                        editItem.BackColor = Color.LightGreen;

                                        break;

                                    case alarm_type.med:
                                        editItem.BackColor = Color.Orange;
                                        break;

                                    case alarm_type.high:
                                        editItem.BackColor = Color.Red;
                                        break;

                                    default:
                                        break;
                                }

                                if (alarmType != alarm_type.normal)
                                {
                                    DialogResult r = MessageBox.Show("温度高于阀值设置，是否产生新告警？", "告警产生", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                                    if (r == DialogResult.Yes)
                                    {
                                        // Report Alarm
                                        Alarm.alarmDetect.instance.Alarm_Report(add_sensorType, alarmType, Convert.ToInt32(tem_Item.MonitorDevice_Id), tem_Item.ID,
                                                                                 textBox_name.Text, textBox_zone.Text,
                                                                                 Convert.ToDouble(tem_Item.Value), Convert.ToDouble(tem_Item.Threshold),
                                                                                 Convert.ToDouble(tem_Item.Threshold_Two), Convert.ToDouble(tem_Item.Threshold_Three));
                                    }
                                }
                            }
                            else
                            {
                                MessageBox.Show("Can't find element in DB!");
                            }

                            break;

                        case sensor_type.humidity:

                            IEnumerable<tb_humidity> humid =
                               from hum in db.tb_humidity
                               where hum.ID == deviceId
                               select hum;

                            tb_humidity hum_Item = null;
                            foreach (tb_humidity item in humid)
                            {
                                hum_Item = item;
                                break;
                            }

                            if (hum_Item != null)
                            {
                                hum_Item.Name = textBox_name.Text;
                                hum_Item.Status = textBox_status.Text;

                                if (textBox_threshold.Text != "")
                                {
                                    hum_Item.Threshold = Convert.ToInt16(textBox_threshold.Text);
                                }

                                if (textBox_threshold2.Text != "")
                                {
                                    hum_Item.Threshold_Two = Convert.ToInt16(textBox_threshold2.Text);
                                }

                                if (textBox_threshold3.Text != "")
                                {
                                    hum_Item.Threshold_Three = Convert.ToInt16(textBox_threshold3.Text);
                                }

                                if (this.checkBox_inUse.Checked == true)
                                {
                                    hum_Item.InUse = true;
                                }
                                else
                                {
                                    hum_Item.InUse = false;
                                }

                                add_db.db.SubmitChanges(ConflictMode.FailOnFirstConflict);

                                editItem.SubItems[2].Text = textBox_name.Text;
                                editItem.SubItems[4].Text = textBox_status.Text;
                                editItem.SubItems[5].Text = textBox_threshold.Text;
                                editItem.SubItems[6].Text = textBox_threshold2.Text;
                                editItem.SubItems[7].Text = textBox_threshold3.Text;

                                if (this.checkBox_inUse.Checked == true)
                                {
                                    editItem.SubItems[9].Text = "True";
                                }
                                else
                                {
                                    editItem.SubItems[9].Text = "False";
                                }

                                // Alarm Type
                                alarm_type alarmType = Alarm.alarmDetect.instance.DetermineAlarmType(add_sensorType, Convert.ToDouble(hum_Item.Value), Convert.ToDouble(hum_Item.Threshold),
                                    Convert.ToDouble(hum_Item.Threshold_Two), Convert.ToDouble(hum_Item.Threshold_Three));

                                // Set Row background color
                                switch (alarmType)
                                {
                                    case alarm_type.normal:
                                        editItem.BackColor = Color.LightCyan;
                                        break;

                                    case alarm_type.mini:
                                        editItem.BackColor = Color.LightGreen;

                                        break;

                                    case alarm_type.med:
                                        editItem.BackColor = Color.Orange;
                                        break;

                                    case alarm_type.high:
                                        editItem.BackColor = Color.Red;
                                        break;

                                    default:
                                        break;
                                }

                                if (alarmType != alarm_type.normal)
                                {
                                    DialogResult r = MessageBox.Show("湿度高于阀值设置，是否产生新告警？", "告警产生", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                                    if (r == DialogResult.Yes)
                                    {
                                        // Report Alarm
                                        Alarm.alarmDetect.instance.Alarm_Report(add_sensorType, alarmType, Convert.ToInt32(hum_Item.MonitorDevice_Id), hum_Item.ID,
                                                                                 textBox_name.Text, textBox_zone.Text,
                                                                                 Convert.ToDouble(hum_Item.Value), Convert.ToDouble(hum_Item.Threshold),
                                                                                 Convert.ToDouble(hum_Item.Threshold_Two), Convert.ToDouble(hum_Item.Threshold_Three));
                                    }
                                    
                                }
                            }
                            else
                            {
                                MessageBox.Show("Can't find element in DB!");
                            }
                            
                            break;

                        case sensor_type.shake:

                            IEnumerable<tb_shake> shake =
                               from sha in db.tb_shake
                               where sha.ID == deviceId
                               select sha;

                            tb_shake shake_Item = null;
                            foreach (tb_shake item in shake)
                            {
                                shake_Item = item;
                                break;
                            }

                            if (shake_Item != null)
                            {
                                shake_Item.Name = textBox_name.Text;
                                shake_Item.Status = textBox_status.Text;

                                if (textBox_threshold.Text != "")
                                {
                                    shake_Item.Threshold = Convert.ToInt16(textBox_threshold.Text);
                                }

                                if (textBox_threshold2.Text != "")
                                {
                                    shake_Item.Threshold_Two = Convert.ToInt16(textBox_threshold2.Text);
                                }

                                if (textBox_threshold3.Text != "")
                                {
                                    shake_Item.Threshold_Three = Convert.ToInt16(textBox_threshold3.Text);
                                }

                                if (this.checkBox_inUse.Checked == true)
                                {
                                    shake_Item.InUse = true;
                                }
                                else
                                {
                                    shake_Item.InUse = false;
                                }

                                add_db.db.SubmitChanges(ConflictMode.FailOnFirstConflict);

                                editItem.SubItems[2].Text = textBox_name.Text;
                                editItem.SubItems[4].Text = textBox_status.Text;
                                editItem.SubItems[5].Text = textBox_threshold.Text;
                                editItem.SubItems[6].Text = textBox_threshold2.Text;
                                editItem.SubItems[7].Text = textBox_threshold3.Text;

                                if (this.checkBox_inUse.Checked == true)
                                {
                                    editItem.SubItems[9].Text = "True";
                                }
                                else
                                {
                                    editItem.SubItems[9].Text = "False";
                                }

                                // Alarm Type
                                alarm_type alarmType = Alarm.alarmDetect.instance.DetermineAlarmType(add_sensorType, Convert.ToDouble(shake_Item.Value), Convert.ToDouble(shake_Item.Threshold),
                                    Convert.ToDouble(shake_Item.Threshold_Two), Convert.ToDouble(shake_Item.Threshold_Three));

                                // Set Row background color
                                switch (alarmType)
                                {
                                    case alarm_type.normal:
                                        editItem.BackColor = Color.LightCyan;
                                        break;

                                    case alarm_type.mini:
                                        editItem.BackColor = Color.LightGreen;

                                        break;

                                    case alarm_type.med:
                                        editItem.BackColor = Color.Orange;
                                        break;

                                    case alarm_type.high:
                                        editItem.BackColor = Color.Red;
                                        break;

                                    default:
                                        break;
                                }

                                if (alarmType != alarm_type.normal)
                                {

                                    DialogResult r = MessageBox.Show("震动值高于阀值设置，是否产生新告警？", "告警产生", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                                    if (r == DialogResult.Yes)
                                    {
                                        // Report Alarm
                                        Alarm.alarmDetect.instance.Alarm_Report(add_sensorType, alarmType, Convert.ToInt32(shake_Item.MonitorDevice_Id), shake_Item.ID,
                                                                                textBox_name.Text, textBox_zone.Text,
                                                                                Convert.ToDouble(shake_Item.Value), Convert.ToDouble(shake_Item.Threshold),
                                                                                Convert.ToDouble(shake_Item.Threshold_Two), Convert.ToDouble(shake_Item.Threshold_Three));
                                    }
                                    
                                }
                            }
                            else
                            {
                                MessageBox.Show("Can't find element in DB!");
                            }
                           
                            break;

                        case sensor_type.voltage:

                            IEnumerable<tb_voltage> volt =
                               from vol in db.tb_voltage
                               where vol.ID == deviceId
                               select vol;

                            tb_voltage vol_Item = null;
                            foreach (tb_voltage item in volt)
                            {
                                vol_Item = item;
                                break;
                            }

                            if (vol_Item != null)
                            {
                                vol_Item.Name = textBox_name.Text;
                                vol_Item.Status = textBox_status.Text;

                                if (textBox_threshold.Text != "")
                                {
                                    vol_Item.Threshold = Convert.ToInt16(textBox_threshold.Text);
                                }

                                if (textBox_threshold2.Text != "")
                                {
                                    vol_Item.Threshold_Two = Convert.ToInt16(textBox_threshold2.Text);
                                }

                                if (textBox_threshold3.Text != "")
                                {
                                    vol_Item.Threshold_Three = Convert.ToInt16(textBox_threshold3.Text);
                                }

                                if (this.checkBox_inUse.Checked == true)
                                {
                                    vol_Item.InUse = true;
                                }
                                else
                                {
                                    vol_Item.InUse = false;
                                }

                                add_db.db.SubmitChanges(ConflictMode.FailOnFirstConflict);

                                editItem.SubItems[2].Text = textBox_name.Text;
                                editItem.SubItems[4].Text = textBox_status.Text;
                                editItem.SubItems[5].Text = textBox_threshold.Text;
                                editItem.SubItems[6].Text = textBox_threshold2.Text;
                                editItem.SubItems[7].Text = textBox_threshold3.Text;

                                if (this.checkBox_inUse.Checked == true)
                                {
                                    editItem.SubItems[9].Text = "True";
                                }
                                else
                                {
                                    editItem.SubItems[9].Text = "False";
                                }

                                // Alarm Type
                                alarm_type alarmType = Alarm.alarmDetect.instance.DetermineAlarmType(add_sensorType, Convert.ToDouble(vol_Item.Value), Convert.ToDouble(vol_Item.Threshold),
                                    Convert.ToDouble(vol_Item.Threshold_Two), Convert.ToDouble(vol_Item.Threshold_Three));

                                // Set Row background color
                                switch (alarmType)
                                {
                                    case alarm_type.normal:
                                        editItem.BackColor = Color.LightCyan;
                                        break;

                                    case alarm_type.mini:
                                        editItem.BackColor = Color.LightGreen;

                                        break;

                                    case alarm_type.med:
                                        editItem.BackColor = Color.Orange;
                                        break;

                                    case alarm_type.high:
                                        editItem.BackColor = Color.Red;
                                        break;

                                    default:
                                        break;
                                }

                                if (alarmType != alarm_type.normal)
                                {
                                    DialogResult r = MessageBox.Show("电压值低于阀值设置，是否产生新告警？", "告警产生", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                                    if (r == DialogResult.Yes)
                                    {
                                        // Report Alarm
                                        Alarm.alarmDetect.instance.Alarm_Report(add_sensorType, alarmType, Convert.ToInt32(vol_Item.MonitorDevice_Id), vol_Item.ID,
                                                                                textBox_name.Text, textBox_zone.Text,
                                                                                Convert.ToDouble(vol_Item.Value), Convert.ToDouble(vol_Item.Threshold),
                                                                                Convert.ToDouble(vol_Item.Threshold_Two), Convert.ToDouble(vol_Item.Threshold_Three));
                                    }
                                    
                                }
                            }
                            else
                            {
                                MessageBox.Show("Can't find element in DB!");
                            }
                            
                            break;

                        default:
                            break;
                    }
                }
                this.Dispose();

            }
            catch(Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                add_db.Refresh_DB();
            }
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void Add_sensorForm_Load(object sender, EventArgs e)
        {
            comboBox_sensorType.Items.Add("智能门锁");
            comboBox_sensorType.Items.Add("温度");
            comboBox_sensorType.Items.Add("湿度");
            comboBox_sensorType.Items.Add("震动");
            comboBox_sensorType.Items.Add("电压");
            comboBox_sensorType.AllowDrop = false;

            if (!b_add)
            {
                //编辑项
                comboBox_sensorType.Enabled = false;
            }

            if (editItem != null)
            {
                switch (add_sensorType)
                {
                    case sensor_type.door:
                        this.textBox_monitorDeviceID.Text = editItem.SubItems[0].Text;
                        this.textBox_deviceID.Text = editItem.SubItems[1].Text;
                        this.textBox_name.Text = editItem.SubItems[2].Text;
                        this.textBox_zone.Text = editItem.SubItems[3].Text;

                        if (editItem.SubItems[4].Text == "前")
                        {
                            this.checkBox_frontDoor.Checked = true;
                            this.checkBox_backDoor.Checked = false;
                        }
                        else if (editItem.SubItems[4].Text == "后")
                        {
                            this.checkBox_frontDoor.Checked = false;
                            this.checkBox_backDoor.Checked = true;
                        }

                        if (editItem.SubItems[5].Text == "开")
                        {
                            this.checkBox_doorOpen.Checked = true;
                            this.checkBox_doorClose.Checked = false;
                        }
                        else if (editItem.SubItems[5].Text == "关")
                        {
                            this.checkBox_doorOpen.Checked = false;
                            this.checkBox_doorClose.Checked = true;
                        }

                        if (editItem.SubItems[6].Text == "True")
                        {
                            this.checkBox_inUse.Checked = true;
                        }
                        else
                        {
                            this.checkBox_inUse.Checked = false;
                        }
                        comboBox_sensorType.SelectedIndex = 0;

                        this.checkBox_backDoor.Visible = true;
                        this.checkBox_frontDoor.Visible = true;
                        this.label_backDoor.Visible = true;
                        this.label_frontDoor.Visible = true;
                        this.checkBox_doorOpen.Visible = true;
                        this.checkBox_doorClose.Visible = true;
                        this.textBox_status.Visible = false;
                        this.label_threshold.Visible = false;
                        this.textBox_threshold.Visible = false;
                        this.textBox_threshold2.Visible = false;
                        this.textBox_threshold3.Visible = false;
                        this.label_threshold2.Visible = false;
                        this.label_threshold3.Visible = false;

                        break;

                    case sensor_type.tempture:
                        this.textBox_monitorDeviceID.Text = editItem.SubItems[0].Text;
                        this.textBox_deviceID.Text = editItem.SubItems[1].Text;
                        this.textBox_name.Text = editItem.SubItems[2].Text;
                        this.textBox_zone.Text = editItem.SubItems[3].Text;
                        this.textBox_status.Text = editItem.SubItems[4].Text;
                        this.textBox_threshold.Text = editItem.SubItems[5].Text;
                        this.textBox_threshold2.Text = editItem.SubItems[6].Text;
                        this.textBox_threshold3.Text = editItem.SubItems[7].Text;

                        if (editItem.SubItems[9].Text == "True")
                        {
                            this.checkBox_inUse.Checked = true;
                        }
                        else
                        {
                            this.checkBox_inUse.Checked = false;
                        }

                        comboBox_sensorType.SelectedIndex = 1;
                        break;

                    case sensor_type.humidity:
                        this.textBox_monitorDeviceID.Text = editItem.SubItems[0].Text;
                        this.textBox_deviceID.Text = editItem.SubItems[1].Text;
                        this.textBox_name.Text = editItem.SubItems[2].Text;
                        this.textBox_zone.Text = editItem.SubItems[3].Text;
                        this.textBox_status.Text = editItem.SubItems[4].Text;
                        this.textBox_threshold.Text = editItem.SubItems[5].Text;
                        this.textBox_threshold2.Text = editItem.SubItems[6].Text;
                        this.textBox_threshold3.Text = editItem.SubItems[7].Text;

                        if (editItem.SubItems[9].Text == "True")
                        {
                            this.checkBox_inUse.Checked = true;
                        }
                        else
                        {
                            this.checkBox_inUse.Checked = false;
                        }

                        comboBox_sensorType.SelectedIndex = 2;
                        break;

                    case sensor_type.shake:
                        this.textBox_monitorDeviceID.Text = editItem.SubItems[0].Text;
                        this.textBox_deviceID.Text = editItem.SubItems[1].Text;
                        this.textBox_name.Text = editItem.SubItems[2].Text;
                        this.textBox_zone.Text = editItem.SubItems[3].Text;
                        this.textBox_status.Text = editItem.SubItems[4].Text;
                        this.textBox_threshold.Text = editItem.SubItems[5].Text;
                        this.textBox_threshold2.Text = editItem.SubItems[6].Text;
                        this.textBox_threshold3.Text = editItem.SubItems[7].Text;

                        if (editItem.SubItems[9].Text == "True")
                        {
                            this.checkBox_inUse.Checked = true;
                        }
                        else
                        {
                            this.checkBox_inUse.Checked = false;
                        }

                        comboBox_sensorType.SelectedIndex = 3;
                        break;

                    case sensor_type.voltage:
                        this.textBox_monitorDeviceID.Text = editItem.SubItems[0].Text;
                        this.textBox_deviceID.Text = editItem.SubItems[1].Text;
                        this.textBox_name.Text = editItem.SubItems[2].Text;
                        this.textBox_zone.Text = editItem.SubItems[3].Text;
                        this.textBox_status.Text = editItem.SubItems[4].Text;
                        this.textBox_threshold.Text = editItem.SubItems[5].Text;
                        this.textBox_threshold2.Text = editItem.SubItems[6].Text;
                        this.textBox_threshold3.Text = editItem.SubItems[7].Text;

                        if (editItem.SubItems[9].Text == "True")
                        {
                            this.checkBox_inUse.Checked = true;
                        }
                        else
                        {
                            this.checkBox_inUse.Checked = false;
                        }

                        comboBox_sensorType.SelectedIndex = 4;
                        break;
                    
                    default:
                        break;
                }
                
            }
        }

        private void textBox_monitorDeviceID_Leave(object sender, EventArgs e)
        {
            try
            {
                if (textBox_monitorDeviceID.Text != "")
                {
                    IEnumerable<tb_monitor_device> queryMon =
                        from mon in add_db.db.tb_monitor_device
                        where Convert.ToInt16(textBox_monitorDeviceID.Text) == mon.ID
                        select mon;

                    if (queryMon.Count() == 0)
                    {
                        MessageBox.Show("没有此交接箱号！");
                    }
                    else
                    {
                        foreach (var item in queryMon)
                        {
                            if (item != null)
                            {
                                textBox_zone.Text = item.Zone;
                                break;
                            }
                        }
                    }

                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void comboBox_sensorType_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 传感器类型选择
            switch (comboBox_sensorType.SelectedIndex)
            {
                case 0:
                    add_sensorType = sensor_type.door;

                    this.checkBox_backDoor.Visible = true;
                    this.checkBox_frontDoor.Visible = true;
                    this.label_backDoor.Visible = true;
                    this.label_frontDoor.Visible = true;
                    this.checkBox_doorOpen.Visible = true;
                    this.checkBox_doorClose.Visible = true;
                    this.textBox_status.Visible = false;
                    this.label_threshold.Visible = false;
                    this.textBox_threshold.Visible = false;
                    this.textBox_threshold2.Visible = false;
                    this.textBox_threshold3.Visible = false;
                    this.label_threshold2.Visible = false;
                    this.label_threshold3.Visible = false;

                    break;

                case 1:
                    add_sensorType = sensor_type.tempture;

                    this.checkBox_backDoor.Visible = false;
                    this.checkBox_frontDoor.Visible = false;
                    this.label_backDoor.Visible = false;
                    this.label_frontDoor.Visible = false;
                    this.checkBox_doorOpen.Visible = false;
                    this.checkBox_doorClose.Visible = false;
                    this.textBox_status.Visible = true;
                    this.label_threshold.Visible = true;
                    this.textBox_threshold.Visible = true;
                    this.textBox_threshold2.Visible = true;
                    this.textBox_threshold3.Visible = true;
                    this.label_threshold2.Visible = true;
                    this.label_threshold3.Visible = true;


                    break;

                case 2:
                    add_sensorType = sensor_type.humidity;

                    this.checkBox_backDoor.Visible = false;
                    this.checkBox_frontDoor.Visible = false;
                    this.label_backDoor.Visible = false;
                    this.label_frontDoor.Visible = false;
                    this.checkBox_doorOpen.Visible = false;
                    this.checkBox_doorClose.Visible = false;
                    this.textBox_status.Visible = true;
                    this.label_threshold.Visible = true;
                    this.textBox_threshold.Visible = true;
                    this.textBox_threshold2.Visible = true;
                    this.textBox_threshold3.Visible = true;
                    this.label_threshold2.Visible = true;
                    this.label_threshold3.Visible = true;

                    break;

                case 3:
                    add_sensorType = sensor_type.shake;

                    this.checkBox_backDoor.Visible = false;
                    this.checkBox_frontDoor.Visible = false;
                    this.label_backDoor.Visible = false;
                    this.label_frontDoor.Visible = false;
                    this.checkBox_doorOpen.Visible = false;
                    this.checkBox_doorClose.Visible = false;
                    this.textBox_status.Visible = true;
                    this.label_threshold.Visible = true;
                    this.textBox_threshold.Visible = true;
                    this.textBox_threshold2.Visible = true;
                    this.textBox_threshold3.Visible = true;
                    this.label_threshold2.Visible = true;
                    this.label_threshold3.Visible = true;

                    break;

                case 4:
                    add_sensorType = sensor_type.voltage;

                    this.checkBox_backDoor.Visible = false;
                    this.checkBox_frontDoor.Visible = false;
                    this.label_backDoor.Visible = false;
                    this.label_frontDoor.Visible = false;
                    this.checkBox_doorOpen.Visible = false;
                    this.checkBox_doorClose.Visible = false;
                    this.textBox_status.Visible = true;
                    this.label_threshold.Visible = true;
                    this.textBox_threshold.Visible = true;
                    this.textBox_threshold2.Visible = true;
                    this.textBox_threshold3.Visible = true;
                    this.label_threshold2.Visible = true;
                    this.label_threshold3.Visible = true;

                    break;

                default:
                    break;
            }
        }

        private void checkBox_frontDoor_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox_frontDoor.Checked == true)
            {
                this.checkBox_backDoor.Checked = false;
            }
            else
            {
                this.checkBox_backDoor.Checked = true;
            }
        }

        private void checkBox_backDoor_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox_backDoor.Checked == true)
            {
                this.checkBox_frontDoor.Checked = false;
            }
            else
            {
                this.checkBox_frontDoor.Checked = true;
            }
        }

        private void checkBox_doorOpen_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox_doorClose_CheckedChanged(object sender, EventArgs e)
        {

        }

    }
}
